package com.warmlight.voicepacket.fragment;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.adapter.MyMessageAdapter;
import com.warmlight.voicepacket.base.BaseFragment;
import com.warmlight.voicepacket.http.HttpClient;
import com.warmlight.voicepacket.http.HttpConfig;
import com.warmlight.voicepacket.http.HttpUtils;
import com.warmlight.voicepacket.http.ResponseData;
import com.warmlight.voicepacket.utils.JSONUtils;

import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

/**
 * 我的系统消息页面
 */
public class MyMessageFragment extends BaseFragment{
    private SmartRefreshLayout refreshLayout;
    private RecyclerView rvMymessagelist;
    //下拉刷新组件
    private TextView mEmptyView;   // 空白视图
    private MyMessageAdapter adapter;
    private int page=0;

    public static MyMessageFragment newInstance(int tag) {
        MyMessageFragment f = new MyMessageFragment();

        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_mymessage, null);

        initview(v);

        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                loadData(true);
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                loadData(false);
            }
        });

        adapter=new MyMessageAdapter(getContext());
        rvMymessagelist.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false));
        rvMymessagelist.setAdapter(adapter);
        loadData(true);


    }

    private void loadData(final boolean isRefresh) {
        if (isRefresh){
            page = 1;
        }else{
            page ++;
        }
        String url = HttpConfig.NEWS_LIST;
        TreeMap<String,String> treeMap = new TreeMap<>();
        treeMap.put("page",page+"");
        treeMap.put("pagesize","10");
        HttpClient.getInstance().postRxAndroidAES(url,treeMap)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseData>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseData responseData) {
                        if (HttpUtils.isCodeSuccess(responseData.getCode())){
                            String data = responseData.getData();
                            ArrayList<Map<String, String>> dataList = JSONUtils.getListMapByJson(data);
                            if (dataList.size()>0){
                                Map<String, String> map = dataList.get(0);
                                String list = map.get("list");
                                ArrayList<Map<String, String>> listLists = JSONUtils.getListMapByJson(list);
                                if (listLists.size()>0){
                                    if (isRefresh){
                                        adapter.setItems(listLists);
                                    }else{
                                        adapter.addItems(listLists);
                                    }
                                    refreshLayout.finishRefresh();
                                    refreshLayout.finishLoadMore();
                                }else{
                                    if (isRefresh){
                                        refreshLayout.finishRefresh();
                                    }else{

                                        refreshLayout.finishLoadMore();
                                    }
                                    refreshLayout.setNoMoreData(true);
                                }
                            }else{
                                refreshLayout.setNoMoreData(true);
                            }
                        }else{
                            if (isRefresh){
                                refreshLayout.finishRefresh(false);
                            }else{

                                refreshLayout.finishLoadMore(false);
                            }
                        }
                        if (adapter.getItemCount()==0){
                            refreshLayout.setVisibility(View.GONE);
                        }else {
                            refreshLayout.setVisibility(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (isRefresh){
                            refreshLayout.finishRefresh(false);
                        }else{

                            refreshLayout.finishLoadMore(false);
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    private void initview(View v) {

        refreshLayout = (SmartRefreshLayout) v.findViewById(R.id.refreshLayout);
        rvMymessagelist = (RecyclerView) v.findViewById(R.id.rv_mymessagelist);
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        dividerItemDecoration.setDrawable(new ColorDrawable(ContextCompat.getColor(getContext(),R.color.color_eee)));
        rvMymessagelist.addItemDecoration(dividerItemDecoration);

    }

}
